How to Unit Test Unmanaged Code
=Unit Testing Tool= Whether or NUnit is used as the unit testing tool, both require that test code reside in a .NET assembly. The following are the methods by which either tool can be used to test unmanaged C++ code. Testing COM objects If the C++ production code is already a COM object, then you simply create a interop assembly for the COM object, and then write your unit tests in C# or that call the COM object methods Examples and Additional details <-- enter more details here. Testing Standard DLLs If the C or C++ production code is in a , with function exports, then you have two options: #Write your unit tests in C# or C++/CLR, and use P/Invoke to call the DLL functions. #Write your unit tests in C++/CLR, and use Examples and Additional details <-- enter more details here. Testing C++ classes using #include If your C++ classes are self contained, then you can write your unit tests in C++/CLR, and directly #include the C++ classes into your unit test code. Note that in this case, the C++ code is actually compiled as managed code. Examples and Additional details <-- enter more details here. Testing C++ classes using exported classes If your C++ production code is in a DLL, you can export the C++ classes using , then write your unit test using C++/CLR, link to the import library of your production DLL, and call the C++ class methods as usual. It's possible that the instantiating a production C++ class from the unit test will not work because of complex header and template dependencies that cannot be easily resolved. The workaround is to add a standard exported function to the production DLL which instantiate the C++ class and returns an instance of it to the unit test. You still need to use __declspec(dllexport) on the C++ class, however, as well as on the exported function. Examples and Additional details <-- enter more details here. Testing EXEs You can't call functions in an EXE from a Test DLL. You'll have to create another project to compile the code into a DLL and then use one of the options above. Examples and Additional details <-- enter more details here. Testing Static Libraries If the is self-contained, you can write your unit test in C++/CLR and link the static library to the unit test directly. But if it's not self-contained, you'll get linker errors, which means the library requires other code that probably resides in another library or in the main program. You'll need to resolve these, which you may be able to by adding those other libraries to your project. Another way is to declare the unresolved symbols in the unit test so that the linker resolves to them. If it's possible to recompile the static-library into a DLL, that would be the preferred solution because then you can use one of the options above that work with DLLs. Even if the production build requires the code to be delivered as a static-library, it may be possible to create another project that compiles the same code into a DLL, for testing purposes only. Examples and Additional details <-- enter more details here. =References= * See Unit Testing Category:Software Design